import Net from "./Net.js";
import Utils from "../utils/Utils.js";
import Db from "../db/Db.js";
import Drawer from "../drawer/Drawer.js";
import { URL } from "../../configure/Assets.js"
import { ServiceType,LayerType } from "../db/Type.js";
export default class Api {
    constructor() {
        if (Api.instance) {
            return Api.instance;
        }
        Api.instance = this;

        this.net = new Net();
        this.utils = new Utils();
        this.drawer = new Drawer();
        this.db = new Db();

        this.BASE_URL = URL.BASE_URL;
        this.TRACK_URL = URL.TRACK_URL;
        this.SAFE_URL = "http://127.0.0.1.8090";
    }

    // 获取Vector
    async getVector(meta_id,meta_version) {
        return await this.net.get(`${this.BASE_URL}/vector`, { meta_id: meta_id,meta_version: meta_version });
    }

    // 获取Vectors
    async getVectors(meta_ids) {
        return await this.net.post(`${this.BASE_URL}/vector`, {meta_ids: meta_ids});
    }

    // 获取Code
    async getCode() {
        return await this.net.get(`${this.BASE_URL}/code`);
    }

    // 获取Config
    async getConfig() {
        return await this.net.get(`${this.BASE_URL}/config`);
    }

    // 获取Track
    async getTracks(track) {
        return await this.net.get(`${this.BASE_URL}/track`, track);
    }

    // 添加Track
    async addTrack(track) {
        return await this.net.post(`${this.BASE_URL}/track`, track);
    }

    // 删除Track
    async delTrack(track) {
        return await this.net.delete(`${this.BASE_URL}/track`, track);
    }

    // 获取Track
    async getPositions(position) {
        return await this.net.get(`${this.BASE_URL}/position`, position);
    }

    // 添加Position
    async addPosition(position) {
        return  await this.net.post(`${this.BASE_URL}/position`, position);
    }

    // 修改Position
    async updPosition(position) {
        return  await this.net.put(`${this.BASE_URL}/position`, position);
    }

    // 删除Position
    async delPosition(obj) {
        return await this.net.delete(`${this.BASE_URL}/position`, obj);
    }

    // 获取Track
    async getWarnings(meta_id) {
        return await this.net.get(`${this.BASE_URL}/warning`, { meta_id: meta_id });
    }

    // 添加Warning
    async addWarning(warning) {
        return  await this.net.post(`${this.BASE_URL}/warning`, warning);
    }

    // 删除Warning
    async delWarning(warning) {
        return await this.net.delete(`${this.BASE_URL}/warning`, warning);
    }

    // 获取Monitor
    async getMonitors(meta_id) {
        return await this.net.get(`${this.BASE_URL}/monitor`, { meta_id: meta_id });
    }

    // 添加Monitor
    async addMonitor(monitor) {
        return  await this.net.post(`${this.BASE_URL}/monitor`, monitor);
    }

    // 删除Monitor
    async delMonitor(monitor) {
        return await this.net.delete(`${this.BASE_URL}/monitor`, monitor);

    }

    // 获取车辆定位信息
    async getVehicle() {
        return await this.net.get(`${this.BASE_URL}/vehicle`);
    }

    // 获取人员定位信息
    async getPerson() {
        return await this.net.get(`${this.BASE_URL}/person`);
    }

    // 获取人员定位信息
    async getReaders() {
        return await this.net.get(`${this.BASE_URL}/reader`);
    }

    // 从网络获取meta
    async getMeta(filter) {
        return await this.net.post(`${this.BASE_URL}/meta`, filter);
    }

    // 更新meta信息
    async updateMeta(meta) {
        return await this.net.put(`${this.BASE_URL}/meta`, meta);
    }

    // 从网络获取metas
    async getMetas(mine_code) {
        return await this.net.get(`${this.BASE_URL}/meta`, { mine_code: mine_code });
    }

    // 从网络获取metas
    async getMetasV2(filter) {
        return await this.net.post(`${this.BASE_URL}/metas`, filter);
    }

    // 删除meta
    async delMeta(meta_id) {
        return await this.net.delete(`${this.BASE_URL}/meta`, { meta_id: meta_id });
    }

    // 获取当前煤矿Meta的结构树
    async getMetasTree(mine_code) {
        return await this.net.get(`${this.BASE_URL}/metas`, { mine_code: mine_code });
    }

    // 上传DWG文件
    async uploadDwg(file) {
        return await this.net.post(`${this.BASE_URL}/dwg`, file, "multipart/form-data");
    }

    // 下载DWG文件
    async downloadDwg(meta_id, name) {
        const data = await this.net.download(`${this.BASE_URL}/dwg`, {meta_id: meta_id});
        this.utils.blob(data, `${name}.dwg`);
    }

    // 上传DXF文件
    async uploadDxf(file) {
        return await this.net.post(`${this.BASE_URL}/dxf`, file, "multipart/form-data");
    }

    // 下载Dxf文件
    async downloadDxf(meta_id, name) {
        const data = await this.net.download(`${this.BASE_URL}/dxf`, { meta_id: meta_id });
        this.utils.blob(data, `${name}.dxf`);
    }

    // 下载备份文件
    async dump() {
        const data = await this.net.download(`${this.BASE_URL}/dump`);
        this.utils.blob(data, "gis.db");
    }

    // 轨迹回放(车辆定位)
    async trackPlayBack(param) {
        // /GIS/GetCarTrack?carid=23331&stime=2020-5-8&etime=2023-5-9
        return await this.net.get(`${this.TRACK_URL}/GIS/GetCarTrack`, param);
    }

    // 轨迹回放(人员定位)
    async trackPlayBack2(param) {
        // /GIS/RfNum?RfNum=uuid&stime=2020-5-8&etime=2023-5-9
        return await this.net.get(`${this.TRACK_URL}/GIS/GetUserTrack`, param);
    }

    // 获取车辆信息
    async getCars() {
        return await this.net.get(`${this.BASE_URL}/car`);
    }

    // 添加车辆与轨迹关联
    async addCarTrack(cartrack) {
        return  await this.net.post(`${this.BASE_URL}/cartrack`, cartrack);
    }

    // 获取车辆与轨迹线关联列表
    async getCarTracks(filter={}) {
        return await this.net.get(`${this.BASE_URL}/cartrack`, filter);
    }

    // 删除车辆与轨迹关联
    async delCarTrack(id) {
        return await this.net.delete(`${this.BASE_URL}/cartrack`, { id: id });
    }

    // 得到传感器信息
    async getSensor() {
        return await this.net.get(`${this.BASE_URL}/sensor`);
    }

    // ---------------------------------------------------------------------------------------------------
    // 通过mine_code获取巷道信息
    async getAllTunnelList(filter) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/getAllTunnelList`;
        return await this.net.post(url, filter);
    }

    // 得到巷道的全部导线点
    async getTunnelPointList(filter) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/getTunnelPointList`;
        return await this.net.post(url, filter);
    }

    // 添加一个导线点
    async insertTunnelPoint(tunnelPoint) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/insertTunnelPoint`;
        return await this.net.post(url, tunnelPoint);
    }

    // 更新巷道的导线点
    async updateTunnelPoint(tunnelPoint) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/updateTunnelPoint`;
        return await this.net.post(url, tunnelPoint);
    }

    // 删除巷道的导线点
    async deleteTunnelPoint(tunnelPoint) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/deleteTunnelPoint`;
        return await this.net.post(url, tunnelPoint);
    }

    // 得到公用的字典Code
    async getCommonDicCode(code) {
        return await this.net.get(`${this.SAFE_URL}/algoapi/common/datadic/${code}`);
    }

    // 插入进尺数据
    async insertDrivageRuler(ruler) {
        return await this.net.post(`${this.SAFE_URL}/algoapi/gis2d/insertDrivageRuler`, ruler);
    }

    // 获取当前矿的全部断层数据
    async getFaultList(code) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/getFaultList`;
        return await this.net.post(url, { mineCode: code });
    }

    // 插入断层数据
    async insertFault(fault) {
        return await this.net.post(`${this.SAFE_URL}/algoapi/gis2d/insertFault`, fault);
    }

    // 修改断层数据
    async updateFault(fault) {
        return await this.net.post(`${this.SAFE_URL}/algoapi/gis2d/updateFault`, fault);
    }

    // 删除断层数据
    async deleteFault(fault) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/deleteFault`;
        return await this.net.post(url, fault);
    }

    // 获取当前煤矿的全部预警面
    async getWarnAreaList(code) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/getWarnAreaList`;
        return await this.net.post(url, { mineCode: code });
    }

    // 插入预警面数据
    async insertWarning(warn) {
        return await this.net.post(`${this.SAFE_URL}/algoapi/gis2d/insertWarnArea`, warn);
    }

    // 修改预警面数据
    async updateWarning(warn) {
        return await this.net.post(`${this.SAFE_URL}/algoapi/gis2d/updateWarnArea`, warn);
    }

    // 删除预警面数据
    async deleteWarning(warnAreaCode) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/deleteWarnArea`;
        return await this.net.post(url, { warnAreaCode: warnAreaCode });
    }

    // 获取灾害测点树
    async getSensorTree(mineCode) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/getDisasterTpTree`;
        return await this.net.post(url, { mineCode: mineCode });
    }

    // 添加传感器坐标
    async updateTpCoord(sensor) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/updateTpCoord`;
        return await this.net.post(url, sensor);
    }

    // 获取瓦斯传感器信息详情
    async getGasTips(monitor) {
        const url = `${this.SAFE_URL}/algoapi/gas/v1/monitor-screen/wasi/gis/nodeInfo`;
        return await this.net.post(url, monitor);
    }

    // 获取瓦斯传感器曲线数据
    async getGasDetails(monitor) {
        const url = `${this.SAFE_URL}/algoapi/gas/v1/monitor-screen/wasi/gis/nodeLine`;
        return await this.net.post(url, monitor);
    }

    // 获取顶板和冲压传感器信息详情
    async getRoofTips(roof) {
        const url = `${this.SAFE_URL}/algoapi/roof/point/getRoofTipInfo`;
        return await this.net.post(url, roof);
    }

    // 获取顶板和冲压传感器曲线数据
    async getRoofDetails(roof) {
        const url = `${this.SAFE_URL}/algoapi/roof/point/getRoofPointDetailInfo`;
        return await this.net.post(url, roof)
    }

    // 获取顶板和冲压传感器信息详情
    async getWaterTips(roof) {
        const url = `${this.SAFE_URL}/algoapi/water/monitor/sensortips/details`;
        return await this.net.post(url, roof);
    }

    // 获取顶板和冲压传感器曲线数据
    async getWaterDetails(roof) {
        const url = `${this.SAFE_URL}/algoapi/water/monitor/sensortips/monitordata`;
        return await this.net.post(url, roof);
    }

    // 获取传感器的Tips
    async getSensorTips(sensor) {
        let data
        if (sensor.disasterType === 1) {
            data = await this.getWaterTips(sensor);
        } else if (sensor.disasterType === 2) {
            data = await this.getGasTips(sensor);
        } else if (sensor.disasterType === 3) {
            data = await this.getRoofTips(sensor);
        }
        return data;
    }

    // 获取传感器的Details
    async getSensorDetails(sensor) {
        let data
        if (sensor.disasterType === 1) {
            data = await this.getWaterDetails(sensor);
        } else if (sensor.disasterType === 2) {
            data = await this.getGasDetails(sensor);
        } else if (sensor.disasterType === 3) {
            data = await this.getRoofDetails(sensor);
        }
        return data;
    }

    // 增加富水区
    async insertWater(water) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/waterenrichedarea`;
        return await this.net.post(url, water);
    }

    // 删除富水区
    async deleteWater(water) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/waterenrichedarea/delete`;
        return await this.net.post(url, water);
    }

    // 获取富水区
    async getWaters(water) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/waterenrichedarea`;
        return await this.net.get(url, water);
    }

    // 增加裂隙带
    async insertFissure(fissure) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/fracturezone`;
        return await this.net.post(url, fissure);
    }

    // 删除裂隙带
    async deleteFissure(fissure) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/fracturezone/delete`;
        return await this.net.post(url, fissure);
    }

    // 获取裂隙带
    async getFissures(fissure) {
        const url = `${this.SAFE_URL}/algoapi/gis2d/fracturezone`;
        return await this.net.get(url, fissure);
    }
    // 直接获取 m_position表格数据
    async getSmPosition(mine_id,node_id,service_type) {
        const url = `${this.BASE_URL}/sm_position`;
        let layer = ''
        switch (service_type) {
            case ServiceType.Gas:
                layer = LayerType.SMGas
                break
            case ServiceType.Video:
                layer = LayerType.SMVideo
                break
            case ServiceType.Staff:
                layer = LayerType.SMStaff
                break
            case ServiceType.BroadCast:
                layer = LayerType.SMBroadCast
                break
            case ServiceType.Ventilate:
                layer = LayerType.SMVentilate
                break
            default:
                break
        }
        return await this.net.get(url,{mine_id,node_id,service_type,layer})
    }

    // 直接获取 m_position 所有数据
    async getSmPositions(mine_id) {
        const url = `${this.BASE_URL}/sm_position`;
        return await this.net.get(url,{mine_id,node_id:null,service_type:null,layer:null})
    }
    // 直接删除 m_position表格数据
    async delSmPosition(info) {
        const url = `${this.BASE_URL}/sm_position`;
        await this.net.delete(url,info)
    }

    // 批量删除 m_position表格数据
    async delSmPositions(infos) {
        const url = `${this.BASE_URL}/sm_position`;
        await this.net.delete(url,infos)
    }

    // 直接修改 m_position表格数据
    async updatePosition(info) {
        const url = `${this.BASE_URL}/sm_position`;
        await this.net.put(url,info)
    }

    // 直接添加 m_position表格数据
    async addSmPosition(info) {
        const url = `${this.BASE_URL}/sm_position`;
        await this.net.post(url,info)
    }

    // 1.从自己的数据库里面获取 断电区域的位置信息
    // 2.从kj83数据库里面获取 断电区域与传感器之间的关联关系
    async getSmPoweredAreas(meta_id='person',mine_id=ServiceType.Mine_Id,layer=LayerType.SMPoweredArea) {
        const url = `${this.BASE_URL}/sm_powered_area`;
        const res = await this.net.get(url,{meta_id,mine_id,layer})
        return res
    }

    async updateFeatureLayer(id,type,layerName) {
        const url = `${this.BASE_URL}/layer_feature`;
        return await this.net.put(url,{id,type,layerName})
    }

    // 修改 多个图纸图层的颜色设置。 让图纸的图层中的元素在gis中渲染成不同的颜色
    async updateLayerRenderColor(meta_id,layerIds,color) {
        const url = `${this.BASE_URL}/layer_color`;
        const res = await this.net.put(url,{meta_id,layerIds,color})
        return res
    }

    // 风向相关的函数
    async addSmVentilate(mine_id,meta_id,node_id,rotation,start_x,start_y,end_x,end_y,ventilateType) {
        const url = `${this.BASE_URL}/ventilation_rotation`;
        const info = {
            mine_id,
            meta_id,
            node_id,
            rotation,
            start_x,
            start_y,
            end_x,
            end_y,
            ventilateType
        }
        return await this.net.post(url,info)
    }

    async deleteSmVentilate(mine_id,meta_id,node_id) {
        const url = `${this.BASE_URL}/ventilation_rotation`;
        const info = {
            mine_id,
            meta_id,
            node_id
        }
        return await this.net.delete(url,info)
    }

    // 获取安全监控系统上 通风传感器的所有风向信息
    async getSmVentilateRotation(meta_id) {
        const url = `${this.BASE_URL}/ventilation_rotation`;
        const res = await this.net.get(url,{meta_id})
        return res
    }

    // 沈阳院 这边相关的接口
    async tree_data(meta_id, type) {
        // meta_id:图纸id, type:系统的类型
        const url = `${this.BASE_URL}/tree_data`;
        const res = await this.net.get(url,{meta_id, type})
        return res
    }

    // 获取当前矿正在井下活动的人员的列表
    async current_staff_data(meta_id) {
        const url = `${this.BASE_URL}/shanxi_staff`;
        const res = await this.net.get(url,{meta_id})
        return res
    }

    // 获取当前矿 监控分站列表
    async current_kstation_data(meta_id) {
        const url = `${this.BASE_URL}/shanxi_kstation`;
        const res = await this.net.get(url,{meta_id})
        return res
    }

    // 获取当前矿 监控测点列表
    async current_knode_data(meta_id) {
        const url = `${this.BASE_URL}/shanxi_knode`;
        const res = await this.net.get(url,{meta_id})
        return res
    }

    // 获取当前矿 工业视频列表
    async current_vcamera_data(meta_id) {
        const url = `${this.BASE_URL}/shanxi_vcamera`;
        const res = await this.net.get(url,{meta_id})
        return res
    }

    async shanxiDevicePosUpdate(devices, metaId) {
        const url = `${this.BASE_URL}/shanxi_device_pos`;
        const res = await this.net.put(url,{devices,metaId})
        return res
    }

    async shanxiDevicePosGet(metaId) {
        const url = `${this.BASE_URL}/shanxi_device_pos`;
        const res = await this.net.get(url,{metaId})
        return res
    }

    async shanxiDevicePosPost(devices, metaId) {
        const url = `${this.BASE_URL}/shanxi_device_pos`;
        const res = await this.net.post(url,{devices,metaId})
        return res
    }

    async shanxiDevicePosDelete(devices, metaId) {
        const url = `${this.BASE_URL}/shanxi_device_pos`;
        const res = await this.net.delete(url,{devices,metaId})
        return res
    }

    async shanxiStaffTrack(meta_id, staff_id) {
        const url = `${this.BASE_URL}/shanxi_staff_track`;
        const res = await this.net.get(url,{meta_id,staff_id})
        return res
    }
}